#define COBJMACROS

#include <windows.h>
#include <objbase.h>
#include <shobjidl.h>

#include "support.h"
#include "window.h"
#include "config.h"

ITaskbarList *taskbar;
HWND win_taskbar;
HWND win_rebar;
HWND win_tasksw;
HWND win_toolbar;
UINT shell_hook_msg;

static HWND require_child(HWND parent, LPCTSTR class)
{
	HWND wnd = FindWindowEx(parent, 0, class, 0);
	if(!wnd) fatal("RQ");
	return wnd;
}

void init_taskbar(void)
{
	win_taskbar = require_child(0, "Shell_TrayWnd");
	win_rebar = require_child(win_taskbar, "ReBarWindow32");
	win_tasksw = require_child(win_rebar, "MSTaskSwWClass");
	win_toolbar = require_child(win_tasksw, "ToolbarWindow32");
	
	CoInitialize(0);
	
	if(CoCreateInstance(
		&CLSID_TaskbarList,
		0,
		CLSCTX_INPROC_SERVER,
		&IID_ITaskbarList,
		(void**)&taskbar
	) != S_OK)
	{
		fatal("Taskbar error");
	}
	
	ITaskbarList_HrInit(taskbar);
	
	shell_hook_msg = RegisterWindowMessage(TEXT("SHELLHOOK"));
	
	if(!shell_hook_msg)
		fatal("Message error");
}

void free_taskbar(void)
{
	ITaskbarList_Release(taskbar);
}

void add_to_taskbar(HWND wnd)
{
	//ITaskbarList_AddTab(taskbar, wnd);
	PostMessage(win_tasksw, shell_hook_msg, HSHELL_WINDOWCREATED, (LPARAM)wnd);
}

void remove_from_taskbar(HWND wnd)
{
	//ITaskbarList_DeleteTab(taskbar, wnd);
	PostMessage(win_tasksw, shell_hook_msg, HSHELL_WINDOWDESTROYED, (LPARAM)wnd);
}
